<?php
/**
 * oscshop2 B2C电子商务系统
 *
 * ==========================================================================
 * @link      http://www.oscshop.cn/
 * @copyright Copyright (c) 2015-2017 oscshop.cn. 
 * @license   http://www.oscshop.cn/license.html License
 * ==========================================================================
 *
 * @author    李梓钿
 *
 */
 
namespace osc\mobile\controller;
use think\Db;
class Tuan extends MobileBase
{

	function index(){
		$this->assign('SEO',array('title'=>'团购-'.config('SITE_TITLE')));	
		$this->assign('top_title','团购');	
		return $this->fetch();
	}
	
	public function ajax_goods_list(){

        $page=input('param.page');//页码

        $limit = (6 * $page) . ",6";
		
		$map['status']=array('eq',1);
		$map['end_time']=array('gt',time());
		
        $list=Db::name('group_buy')->where($map)->order('id desc')->limit($limit)->select();

		$this->assign('list',$list);
		exit($this->fetch());
     
    }
	
	//商品详情
	function detail(){
		
		cookie('jump_url',request()->url(true));
		
		if(!$list=osc_service('common','Tuan')->get_tuan_info((int)input('param.id'))){
			$this->error('商品不存在！！');
		}
		
		$this->assign('top_title',$list['tuan']['group_name']);
		$this->assign('SEO',array('title'=>$list['tuan']['group_name'].'-'.config('SITE_TITLE')));
		
		$this->assign('tuan',$list['tuan']);
		
		$this->assign('options',$list['options']);
	
		return $this->fetch();	
	}
	//取得商品描述
	function get_description(){
		
		$list=osc_service('common','Tuan')->get_tuan_info((int)input('param.id'));
		
		$this->assign('description',$list['image']);
		
		exit($this->fetch());
	}
	function add(){
		if(!$uid=is_login()){
			return array('error'=>'请先登录','url'=>url('Login/login'));
		}
		$cart=osc_cart();
		
		$post=input('post.');
	
		$param['goods_id']=$post['goods_id'];
		$param['quantity']=1;
		$param['option']=isset($post['option'])?$post['option']:'';
		
		//验证商品数量和必选项
		if($error=$cart->check_quantity($param)){			
			return $error;
		}		
		//验证购买条件
		$error=osc_service('common','Tuan')->check_buy_limit($post['id'],$uid);
		
		if(isset($error['error'])){			
			return array('error'=>$error['error']);
		}
		
		$cart_data['uid']=$uid;
		$cart_data['goods_id']=(int)$param['goods_id'];
		$cart_data['quantity']=1;		
		$cart_data['type']=config('tuan_goods');
		$cart_data['goods_option']= isset($post['option'])?json_encode(array_filter($post['option'])):'';
		$cart_data['group_buy_id']=$post['id'];	
	
		if($cart_old=Db::name('cart')->where($cart_data)->find()){//已经存在数据
		
			return array('success'=>url('mobile/tuan/checkout',array('id'=>hashids()->encode($cart_old['cart_id']))));
		
		}else{
			//插入一条数据			
			$cart_data['create_time']=time();	
					
			if($cart_id=Db::name('cart')->insert($cart_data,false,true)){
				return array('success'=>url('mobile/tuan/checkout',array('id'=>hashids()->encode($cart_id))));
			}else{
				return array('error'=>'异常错误');
			}
		}
	}

	function checkout(){
	
		if(!$uid=is_login()){
			return array('error'=>'请先登录','url'=>url('Login/login'));
		}
		
		$cart=osc_cart();
		
		$id=hashids_decode(input('param.id'));
		
		if(!$id){
			$this->error('非法操作');
		}	
		
		if(in_wechat()){
			
			session('jssdk_tuan_order',null);
			//微信支付
			$this->assign('signPackage',wechat()->getJsSign(request()->url(true)));			
			
		}
			
		// 验证商品数量		
		$goods=Db::name('cart')->where('cart_id',$id)->find();		
		
		if(!$goods){
			$this->error('非法操作');
		}
		
		$group_buy=Db::name('group_buy')->where('id',$goods['group_buy_id'])->find();	
		
		$this->assign('goods',$group_buy);		
		
		$address_id=(int)member('address_id');
		//配送地址		
		$address=Db::name('address')->where('address_id',$address_id)->find();				
		//计算运费
		if(isset($address)){
			$this->assign('address',$address);				
		}
		
		$goods_option=osc_cart()->get_cart_options($goods['goods_id'],json_decode($goods['goods_option']));
		
		$this->assign('option',$goods_option);
		
		$this->assign('SEO',array('title'=>'团购结算-'.config('SITE_TITLE')));
		$this->assign('top_title','团购结算');
		
		return $this->fetch();
	}

}
